package wqh.controller.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.XkDevice;
import wqh.global.GlobalXkDevice;
import wqh.pojo.MapRet;
import wqh.service.XkDeviceService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by devts on 2017/1/5.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/xk_device")
public class XkDeviceJSON {

    @Autowired
    private XkDeviceService xkDeviceService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody XkDevice xkDevice){
        if(xkDevice.getIp() == null) xkDevice.setIp("0.0.0.0");
        try {
            xkDeviceService.save(xkDevice);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestBody XkDevice xkDevice){
        try {
            xkDeviceService.delete(xkDevice);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody XkDevice xkDevice){
        try {
            XkDevice x = xkDeviceService.findById(xkDevice.getId());
            if(x == null)   return MapRet.SQL_UPDATE_ERROR();
            if(xkDevice.getIp() != null) x.setIp(xkDevice.getIp());
            if(xkDevice.getName() != null) x.setName(xkDevice.getName());
            if(xkDevice.getDevice_id() != null) x.setDevice_id(xkDevice.getDevice_id());
            if(xkDevice.getPhone() != null) x.setPhone(xkDevice.getPhone());
            if(xkDevice.getDetail() != null) x.setDetail(xkDevice.getDetail());
            xkDeviceService.update(x);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/getAllStatus",method = RequestMethod.GET)
    public Object getAllStatus(){
        try {
            List<XkDevice> xkDeviceList = xkDeviceService.findAll();
            if(xkDeviceList.size() == 0) return MapRet.DATA(null);
            List<Map<String, Object>> list = new ArrayList<>();
            for(XkDevice x : xkDeviceList){
                Map<String, Object> map = new HashMap<>();
                map.put("id",x.getId());
                map.put("device_id",x.getDevice_id());
                map.put("name",x.getName());
                map.put("ip",x.getIp());
                map.put("phone",x.getPhone());
                map.put("detail",x.getDetail());
                if(GlobalXkDevice.exist(x.getDevice_id())) map.put("online",true);
                else map.put("online",false);
                list.add(map);
            }
            return MapRet.DATA(list);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

    @RequestMapping(value = "/getStatusByDeviceId",method = RequestMethod.POST)
    public Object getStatusByDeviceId(@RequestBody XkDevice xkDevice){
        if(xkDevice.getDevice_id() == null) return MapRet.DATA(null);
        XkDevice x = xkDeviceService.findByDeviceID(xkDevice.getDevice_id());
        if(x == null) return MapRet.DATA(null);
        Map<String,Object> map = new HashMap<>();
        map.put("id",x.getId());
        map.put("device_id",x.getDevice_id());
        map.put("name",x.getName());
        map.put("ip",x.getIp());
        if(GlobalXkDevice.exist(x.getDevice_id())) map.put("online",true);
        else map.put("online",false);
        return MapRet.DATA(map);
    }



}
